<?php

namespace App\Http\Controllers\Admin;


use App\Http\Model\Config;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class ConfigController extends CommonController
{
    public function index()
    {
        
        $data= Config::orderBy('conf_order','asc')->get();
        foreach ($data as $k=>$v){
        switch ($v->field_type){
            case 'input':
                $data[$k]->_html= '<input type="text" class="lg" name="conf_content[]" value="'.$v->conf_content.'">';
                break;
            case 'textarea':
                $data[$k]->_html= '<textarea type="text" class="lg" name="conf_content[]" >'.$v->conf_content.'</textarea>';
                //echo $data->_html;
                break;
            case 'radio':
                $arr=explode(',',$v->field_value);
                $str='';
                $c='';
                foreach ($arr as $m=>$n){
                    //1|开启
                    $res=explode('|',$n);
                    $c=$v->conf_content == $res[0]?'checked':'';
                   $str.= '<input type="radio" name="conf_content[]" '.$c.' value="'.$res[0].'" >' .$res[1].'　';

                }
                $data[$k]->_html=$str;
                break;

    }
        }
        return view('admin.config.index',compact('data'));
    }
    //表单提交项处理
    public function changeContent()
    {
        $input=Input::all();
        foreach ($input['conf_id'] as $k=>$v){
            Config::where('conf_id',$v)->update(['conf_content'=>$input['conf_content'][$k]]);
        }
        $this->show();
        return back()->with('errors','配置项内容更新成功！');

    }

    //get 方法 把配置项写入到配置文件中
    public function show()
    {   //读取web配置项里面属性
        //echo \Illuminate\Support\Facades\Config::get('web.web_status');
        $config= Config::pluck('conf_content','conf_name')->all();
        //echo var_export($config,true);
        $path=base_path().'\config\web.php';
        $str='<?php return '.var_export($config,true).';';

        file_put_contents($path,$str);


    }

    //配置项排序
    public function changeOrder()
    {

        $input=Input::all();
        $config=Config::find($input['conf_id']);
        $config->conf_order=$input['conf_order'];
        $res=$config->update();
        if($res){
            $data=[
                'status'=>0,
                'msg'=>'配置项排序更新成功',
            ];
        }else{
            $data=[
                'status'=>1,
                'msg'=>'配置项排序更新失败',
            ];
        }
        return $data;
    }


    //get 方法 admin/Config/create 路由 添加配置项
    public function create()
    {
        return view('admin.config.add');
    }

    //post方法 admin/Config 路由 添加链接  提交
    public function store()
    {
        //打印除token值以外的数据
        if($input=Input::except('_token')) {
                //dd($input);
            $rules = array(
                'conf_name' => 'required ',
                'conf_title' => 'required ',
            );
            $mes=array(
                'conf_name.required'=>'配置名称不能为空',
                'conf_title.required'=>'配置标题不能为空',
            );

            $validator = Validator::make($input, $rules, $mes);

            if ($validator->passes()) {
                $res=Config::create($input);
                if($res){
                    return redirect('admin/config');
                }else{
                    return back()->with('errors','配置项添加失败，请稍后重试！');
                }
            } else {
                return back()->withErrors($validator);
            }
        }

    }

    //get 方法  admin/Config/{Config}/edit 路由配置项编辑
    public function edit($conf_id)
    {
        $field=Config::find($conf_id);
        return view('admin.config.edit',compact('field'));
    }

    //PUT 方法 admin/Config/{Config} 路由
    public function update($conf_id)
    {
        $input=Input::except('_token','_method');
        $res=Config::where('conf_id',$conf_id)->update($input);

        if($res){
            $this->show();
            return redirect('admin/config');
        }else{
            return back()->with('errors','配置项更新失败，请稍后重试！');
        }

    }
    //delete 方法 admin/Config/{Config} 路由 删除配置项
    public function destroy($conf_id)
    {
        $re = Config::where('conf_id',$conf_id)->delete();
        if($re){
            $this->show();
            $data = [
                'status' => 0,
                'msg' => '配置项删除成功！',
            ];
        }else{
            $data = [
                'status' => 1,
                'msg' => '配置项删除失败，请稍后重试！',
            ];
        }
        return $data;
    }



}
